﻿using System;

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using MvcContrib.TestHelper;
using NUnit.Framework;
using Rhino.Mocks;
using Spaanjaars.ContactManager.Infrastructure.UnitOfWork;
using Spaanjaars.ContactManager.Model;
using Spaanjaars.ContactManager.Services.Commands;
using Spaanjaars.ContactManager.Services.Interfaces;
using Spaanjaars.ContactManager.UX.MVC.Controllers;

namespace Spaanjaars.ContactManager.Tests.Controllers
{
    [TestFixture]
    public class When_sucessfully_creating_a_contact_person
    {
        private MockRepository _mocks;
        private IContactPersonCreationService _contactPersonCreationService;

        [SetUp]
        public void Context()
        {
            _mocks = new MockRepository();

            _contactPersonCreationService = _mocks.StrictMock<IContactPersonCreationService>();
        }

        [Test]
        public void Should_call_the_service_CreatePersonFrom_method()
        {
            CreateContactPersonCommandResult createContactPersonCommandResult = new CreateContactPersonCommandResult();

            using (_mocks.Record())
            {
                Expect.Call(_contactPersonCreationService.CreatePersonFrom(null)).IgnoreArguments().Return(createContactPersonCommandResult);
            }
            using (_mocks.Playback())
            {

                var controller = new ContactPersonAddController(_contactPersonCreationService);

                controller.Index(new CreateContactPersonCommand()
                                     {
                                         DateOfBirth = DateTime.Now,
                                         FirstName = "Scott",
                                         MiddleName = string.Empty,
                                         LastName = "Millett"
                                     });
            }

        }

        [Test]
        public void Should_redirect_to_view_all_contact_people()
        {
            CreateContactPersonCommandResult createContactPersonCommandResult = new CreateContactPersonCommandResult();

            using (_mocks.Record())
            {
                Expect.Call(_contactPersonCreationService.CreatePersonFrom(null)).IgnoreArguments().Return(createContactPersonCommandResult);
            }
            using (_mocks.Playback())
            {

                var controller = new ContactPersonAddController(_contactPersonCreationService);

                var response = controller.Index(new CreateContactPersonCommand()
                {
                    DateOfBirth = DateTime.Now,
                    FirstName = "Scott",
                    MiddleName = string.Empty,
                    LastName = "Millett"
                });
                
                response.AssertActionRedirect().ToController("ContactPersonList").ToAction("Index");

            }
        }
    }
}
